package com.huluobo.a2204parkdemo.news

import android.content.Intent
import android.view.View
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import com.huluobo.a2204parkdemo.databinding.ActivityNewsBinding
import com.huluobo.a2204parkdemo.pagingAdapter.NewsPagingAdapter
import com.huluobo.base.mvvm.BaseActivity
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.launch

@AndroidEntryPoint
class NewsActivity : BaseActivity<ActivityNewsBinding, NewsViewModel>() {
    //    private lateinit var newsAdapter: NewsAdapter
    private val pagingAdapter = NewsPagingAdapter()
    override fun initData() {
//        val map = mutableMapOf<String, Int>()
//        map[Const.PARAM_PAGE] = 0
//        val json = Gson().toJson(map)
//        val body = RequestBody.create(Const.MEDIA_TYPE.toMediaTypeOrNull(), json)
//        viewModel.getAllNews(body)
//
//        viewModel.getAllNewsSuccess.observe(this) {
//            val list = Gson().fromJson(it, NewsBean::class.java)
//            newsAdapter.data.clear()
//            newsAdapter.data.addAll(list)
//            newsAdapter.notifyDataSetChanged()
//        }
    }

    override fun initView() {
        binding.newsHeader.headerTitle.text = "新闻"
        binding.newsHeader.headerBack.setOnClickListener { finish() }
        binding.newsHeader.headerAdd.visibility = View.VISIBLE
        binding.newsHeader.headerAdd.setOnClickListener { startActivity(Intent(this, AddNewsActivity::class.java)) }

        binding.newsRv.layoutManager = LinearLayoutManager(this)
//        newsAdapter = NewsAdapter()
//        binding.newsRv.adapter = newsAdapter

        binding.newsRv.adapter = pagingAdapter

        lifecycleScope.launch {
            viewModel.getNewPagingData().collect {
                pagingAdapter.submitData(it)
            }
        }
    }

    override fun onResume() {
        super.onResume()
//        val map = mutableMapOf<String, Int>()
//        map[Const.PARAM_PAGE] = 0
//        val json = Gson().toJson(map)
//        val body = RequestBody.create(Const.MEDIA_TYPE.toMediaTypeOrNull(), json)
//        viewModel.getAllNews(body)
    }
}